﻿
; (function () {

    var page = {
        relativeUrl: '',
        finishList: [],
        unFinishList: [],
        printTotal: 0,
        recommendInfo: {},//推荐的预约信息
        timesPageNum: 0,
        dateIndex: 0,
        editSchedule: true,

        //页面初始化
        init: function () {
            page.finishList.current = 0;
            page.unFinishList.current = 0;
            page.relativeUrl =  $('#hdnRelativeUrl').val();
            page.schedule = new Schedule("Self", page.relativeUrl);
            pageEvent.init();
            page.schedule.days = 3;
            page.schedule.getExamRequestList(page.getExamRequestListCallback);
        },
        
        //获取申请单列表+1
        getExamRequestListCallback: function (data) {
            page.schedule.examRequestList = data;
            page.schedule.scheduleOperatorName = data[0].Name
            $('.b_name').text(data[0].Name);
            $('.b_sex').text( data[0].Sex);
            $('.b_age').text(data[0].Age + data[0].AgeUnit)
            $('.b_department').text(data[0].RequestDeptName)
            for (var i = 0; i < data.length; i++) {
                if (data[i].ScheduleStatus == 0) {
                    page.finishList.push(data[i]);
                    //是否大于两条
                    if (page.finishList.length < 3) {
                        page.bindFinish(data[i], page.finishList.length);
                    } 
                } else {
                    page.unFinishList.push(data[i]);
                    //是否大于两条
                    if (page.unFinishList.length < 3) {
                        page.bindUnFinish(data[i], page.unFinishList.length);
                    } 
                }
            }
            if(page.finishList.length > 2){
                $('.finishright img').css('visibility', 'visible');
            }
            $('.booknum').text('（' + page.finishList.length + '）');

            if(page.unFinishList.length > 2){
                $('.unfinishright img').css('visibility', 'visible');
            }
            $('.unbooknum').text('（' + page.unFinishList.length + '）');
        },

        //未预约+1
        bindUnFinish: function (exam, i) {
            if(!exam){
                return;
            }
            var temp = '<div class="ifbook_items pull-left" data-placerorderno=' + exam.PlacerOrderNO + '>' +
                            '<div class="row check_detail">' +
                                '<table>' +
                                    '<tr>' +
                                        '<td colspan="2">' + i + '.<span class="apply_procedure-name">' + exam.ServiceSectID + "  " + exam.ProcedureName + '<span></td>' +
                                    '</tr>' +
                                    '<tr>' +
                                        '<td class="apply_time">申请时间:</td>' +
                                        '<td>' + imcis.convertDateFromJson(exam.RequestedDate) + '</td>' +
                                    '</tr>' +
                                    '<tr>' +
                                        '<td class="apply_room">申请科室:</td>' +
                                        '<td>' + exam.RequestDeptName + '</td>' +
                                    '</tr>' +
                                '</table>' +
                            '</div>' +
                        '<div class="start_boook unbook_itemslist">选择预约时间</div>' +
                    '</div>';
            $('#main_container .un-finish-container').append(temp);
        },

        //已预约+1
        bindFinish: function (exam, i) {
            if(!exam){
                return;
            }
            var startDate = imcis.convertDateFromJson(exam.ScheduleStartDate);
            var endDate = imcis.convertDateFromJson(exam.ScheduleEndDate);
            var scheduleTime = startDate.substr(0, 16) + "-" + endDate.substr(11, 5);
            var temp = '<div class="ifbook_items pull-left" style="position:relative" data-placerorderno=' + exam.PlacerOrderNO + '>' +
                       ' <div class="print_img" style="display:' + (exam.printFlag ? 'block' : 'none') + '"></div>' +
                        '<div class="row check_detail">' +
                            '<table>' +
                                '<tr>' +
                                    '<td colspan="2">' + i + '.<span class="apply_procedure-name">' + exam.ServiceSectID + "  " + exam.ProcedureName + '<span></td>' +
                                '</tr>' +
                                '<tr>' +
                                    '<td class="apply_time">' + scheduleTime + '</td>' +
                                '</tr>' +
                                '<tr>' +
                                    '<td class="apply_add">' + exam.ScheduleResourceName + "-" + exam.ObservationLocation + '</td>' +
                                '</tr>' +
                            '</table>' +
                        '</div>' +
                        '<div class="change_items">' +
                            '<div class="pull-left edit_book">修改预约时间</div>' +
                            '<div class="pull-left cancel_book">取消预约</div>' +
                        '</div>' +
                    '</div>';

            $('#main_container .finish-container').append(temp);
        }
    };

    var pageEvent = {
        //事件初始化
        init: function () {
            //取消预约+1
            $('#main_container').on('click', '.cancel_book', pageEvent.cancelSchedule);
            //修改预约时间
            $('#main_container').on('click', '.edit_book', pageEvent.editSchedule);
            //未预约开始预约
            $('#main_container').on('click', '.unbook_itemslist', pageEvent.startSchedule);

            //一键预约多个检查申请+1
            $('#saveMulti').on('click', function () {
                if (page.unFinishList.length == 0) {
                    page.schedule.alert('目前您没有未预约的检查单!');
                } else {
                    page.schedule.comfirm('是否一键预约保存!', function () {
                        pageEvent.saveMultiExamSchedule();
                    });  
                }
            });
            //保存预约+1
            $('#save_time').on('click',   pageEvent.saveExamSchedule);

            //切换日期
            $('#time_container').on('click', '.day_items', pageEvent.changeDate);
            //左右移动切换日期
            $('.date-arrow_left').on('click',function () {
                page.schedule.loading();
                pageEvent.dateMoveLeft();
                page.schedule.removeLoading();
            });
            $('.date-arrow_right').on('click', function () {
                page.schedule.loading();
                pageEvent.dateMoveRight();
                page.schedule.removeLoading();
            } );
            //切换时间组
            $('.ampm_container').on('click', '.ampm', function () {
                page.schedule.loading();
                pageEvent.changeGroup();
                page.schedule.removeLoading();
            });
            //选中时间
            $('#time_container').on('click', '.schedule-enable', pageEvent.selectTime);

            //时间页面返回+1
            $('#time_container').on('click', '#backTo', function () {
                $('#time_container').hide();
                $('#main_container').show();
            });

            //选中打印的条数
            $('.finish-container').on('click', '.check_name, .check_detail,.print_img ', pageEvent.selectPrint);
            //打印
            $('#print').on('click', pageEvent.print);

            //左右移动
            $('#main_container').on('click', '.arrow_position', pageEvent.moveSchedule);
            
            //预约时间段上一页
            $('#time_container').on('click', '#uppage', function () {
                if (page.pageNum <= 1) { return false } else {
                    page.pageNum = page.pageNum - 1;
                    pageEvent.addGroupTime(page.recommendInfo.scheduleArrange.groupList[0], page.timesPageNum);
                }
            });
            //预约时间段下一页
            $('#time_container').on('click', '#nextpage', function () {
                    page.pageNum = page.pageNum + 1;
                    pageEvent.addGroupTime(page.recommendInfo.scheduleArrange.groupList[0], page.timesPageNum);
            });
        },
       
        //取消预约+1
        cancelSchedule: function () {
            var $this = $(this);
            page.schedule.comfirm('是否取消预约', function () { 
                var placerOrderNO = $this.closest('.ifbook_items').data("placerorderno");
                var exam = null, index = 0;
                $.each(page.finishList, function (i, n) {
                    if (n.PlacerOrderNO == placerOrderNO) {
                        exam = n;
                        index = i;
                        return false;
                    }
                });
                page.schedule.cancelSchedule({
                        'PlacerOrderNO': placerOrderNO
                    }, function (data) {
                    if(exam.printFlag){
                        exam.printFlag = false;
                        page.printTotal--;
                        $('#print').text('打印' + (page.printTotal <= 0 ? "" : "（" + page.printTotal + "）"));
                    }

                    page.unFinishList.push(exam);
                    page.finishList.splice(index, 1);

                    $('.booknum').text('（' + page.finishList.length + '）');
                    $('.unbooknum').text('（' + page.unFinishList.length + '）');

                    if (page.unFinishList.length <= 2) {
                        page.bindUnFinish(exam, page.unFinishList.length);
                    }
                    if(page.unFinishList.length  > (page.unFinishList.current + 1) * 2){
                        $('.unfinishright img').css('visibility', 'visible');
                    }

                    $('#main_container .finish-container').html('');
                    if (page.finishList.length >= (page.finishList.current + 1) * 2) {
                        page.bindFinish(page.finishList[page.finishList.current * 2], page.finishList.current * 2 + 1);
                        page.bindFinish(page.finishList[page.finishList.current * 2 + 1], page.finishList.current * 2 + 2);
                    } else if (page.finishList.length > page.finishList.current * 2 && page.finishList.length < (page.finishList.current + 1) * 2) {
                        page.bindFinish(page.finishList[page.finishList.current * 2], page.finishList.current * 2 + 1);
                    } else {
                        if (page.finishList.current > 0) {
                            page.finishList.current--;
                            page.bindFinish(page.finishList[page.finishList.current * 2], page.finishList.current * 2 + 1);
                            page.bindFinish(page.finishList[page.finishList.current * 2 + 1], page.finishList.current * 2 + 2);
                        }
                    }
                    if (page.finishList.length <= 2) {
                        $('.finishright').find('img').css('visibility', 'hidden');
                        $('.finishleft').find('img').css('visibility', 'hidden');
                    }
                });
            });
        },

        //修改预约时
        editSchedule: function () {
            $('#main_container').hide();
            $('#time_container').show();
            page.editSchedule = true;
            var placerOrderNO = $(this).closest('.ifbook_items').data('placerorderno');
            $.each(page.finishList, function (i, n) {
                if (n.PlacerOrderNO == placerOrderNO) {
                    n.printFlag = !n.printFlag;
                    page.scheduleExamRequest = n;
                    return false;
                }
            });

            $('.ServiceSectID').text(page.scheduleExamRequest.ServiceSectID);
            $('.ProcedureName').text(page.scheduleExamRequest.ProcedureName);

            page.schedule.getScheduleInfo(page.scheduleExamRequest, [], "", "", pageEvent.getScheduleInfoCallback);
        },

        //开始预约
        startSchedule: function () {
            $('#main_container').hide();
            $('#time_container').show();
            page.editSchedule = false;
            var placerOrderNO = $(this).closest('.ifbook_items').data('placerorderno');
            $.each(page.unFinishList, function (i, n) {
                if (n.PlacerOrderNO == placerOrderNO) {
                    n.printFlag = !n.printFlag;
                    page.scheduleExamRequest = n;
                    return false;
                }
            });

            $('.ServiceSectID').text(page.scheduleExamRequest.ServiceSectID);
            $('.ProcedureName').text(page.scheduleExamRequest.ProcedureName);

            page.schedule.getScheduleInfo(page.scheduleExamRequest, [], "", "", pageEvent.getScheduleInfoCallback);
        },

        //保存单个预约
        saveExamSchedule: function () {
            page.scheduleExamRequest.ResourceUID = page.currentRecommend.ResourceUID;
            page.scheduleExamRequest.ResourceName = page.currentRecommend.ResourceName;
            page.scheduleExamRequest.ScheduleDate = page.currentRecommend.ScheduleDate;
            page.scheduleExamRequest.ScheduleTime = page.currentRecommend.ScheduleTime;
            page.scheduleExamRequest.Quantity = page.currentRecommend.Quantity;

            page.schedule.saveExamSchedule(page.scheduleExamRequest, function (data) {
                var placerOrderNO = page.scheduleExamRequest.PlacerOrderNO;
                if (data && data.length > 0) {
                    $.each(data, function (i, n) {
                        if (placerOrderNO == n.PlacerOrderNO) {
                            page.scheduleExamRequest.ScheduleResourceUID = n.ScheduleResourceUID;
                            page.scheduleExamRequest.ScheduleResourceName = n.ScheduleResourceName;
                            page.scheduleExamRequest.ScheduleStartDate = n.ScheduleStartDate;
                            page.scheduleExamRequest.ScheduleEndDate = n.ScheduleEndDate;
                            page.scheduleExamRequest.ObservationLocation = n.ObservationLocation;
                            page.scheduleExamRequest.ScheduleFlag = true;

                            return false;
                        }
                    });
                }

                //检查申请的保存
                if (!page.editSchedule) {
                    $('.ifbook_items[data-PlacerOrderNO="' + placerOrderNO + '"]').remove();

                    $.each(page.unFinishList, function (i, n) {
                        if (n.PlacerOrderNO == placerOrderNO) {
                            page.scheduleExamRequest.printFlag = false;
                            page.bindFinish(page.scheduleExamRequest, page.finishList.length);
                            page.finishList.push(page.scheduleExamRequest);
                            page.unFinishList.splice(i, 1);
                            pageEvent.bindAfterSave();
                            return false;
                        }
                    });
                } else {
                    //已预约的修改
                    $.each(page.unFinishList, function (i, n) {
                        if (n.PlacerOrderNO == placerOrderNO) {
                            n.ScheduleResourceUID = page.scheduleExamRequest.ScheduleResourceUID;
                            n.ScheduleResourceName = page.scheduleExamRequest.ScheduleResourceName
                            n.ScheduleStartDate =  page.scheduleExamRequest.ScheduleStartDate;
                            n.ScheduleEndDate = page.scheduleExamRequest.ScheduleEndDate;

                            var $item = $('.ifbook_items[data-PlacerOrderNO="' + placerOrderNO + '"]');
                            $item.find('.apply_time').text(page.scheduleExamRequest.ScheduleDate + " " + page.scheduleExamRequest.StartTime + "-" + page.scheduleExamRequest.EndTime);
                            $item.find('.apply_add').text(page.scheduleExamRequest.ScheduleResourceName + "-" + (page.scheduleExamRequest.ObservationLocation || ""));

                            return false;
                        }
                    });
                }

                $('#main_container').show();
                $('#time_container').hide();
               
            });
        },

        //一键预约多个检查申请
        saveMultiExamSchedule: function () {
            imcis.ajax(page.relativeUrl + 'SaveExamScheduleAuto', page.unFinishList, true, function (data) {
                if (data && data.success) {
                    var errorMsg = "";
                    for (var i = 0; i < data.resultList.length; i++) {
                        if (data.resultList[i].ErrorMsg) {
                            errorMsg += data.resultList[i].ErrorMsg;
                        } else {
                            var exam = null, index = 0;
                            $.each(page.unFinishList, function (idx, n) {
                                if (n.PlacerOrderNO == data.resultList[i].PlacerOrderNO) {
                                    exam = n;
                                    index = idx;
                                    return false;
                                }
                            });

                            exam.ScheduleID = data.resultList[i].ScheduleResultInfo.ScheduleID;
                            exam.ScheduleResourceUID = data.resultList[i].ScheduleResultInfo.ScheduleResourceUID;
                            exam.ScheduleResourceName = data.resultList[i].ScheduleResultInfo.ScheduleResourceName;
                            exam.ScheduleStartDate = data.resultList[i].ScheduleResultInfo.ScheduleStartDate;
                            exam.ScheduleEndDate = data.resultList[i].ScheduleResultInfo.ScheduleEndDate;
                            exam.ScheduleNotice = data.resultList[i].ScheduleResultInfo.ScheduleNotice;
                            exam.ObservationLocation = data.resultList[i].ScheduleResultInfo.ResourceLocation;
                            exam.printFlag = false;

                            page.finishList.push(exam);
                            page.unFinishList.splice(index, 1);
                        }
                    }

                    pageEvent.bindAfterSave();

                    if (errorMsg) {
                        page.schedule.alert(errorMsg);
                    }
                } else {
                    page.schedule.alert('一键预约失败，请重试！');
                   
                }
            });
        },

        //获取单个申请的预约资源排班+1
        getScheduleInfoCallback: function (data) {
            page.currentRecommend = {};
            page.currentRecommend.Quantity = data.Quantity;
            page.currentRecommend.ResourceName = data.ResourceName;
            page.currentRecommend.ResourceUID = data.ResourceUID;
            page.currentRecommend.ScheduleDate = data.ScheduleDate;
            page.currentRecommend.ScheduleTime = data.ScheduleTime;
            page.currentRecommend.ScheduleArrangeList = [];
            $.each(data.ScheduleArrangeResourceInfoList, function (i, n) {
                if (n.ResourceUID == data.ResourceUID) {
                    $('#time_container .day_items').removeClass('actived_item');
                    $('#time_container .day_0').addClass('actived_item');

                    page.currentRecommend.ObservationLocation = n.ResourceLocation;

                    for (var j = 0; j < n.ScheduleArrangeDateInfoList.length; j++) {
                        var arrangeDay = imcis.convertDateFromJson(n.ScheduleArrangeDateInfoList[j].ArrangeDay, true);
                        $('#time_container .day_' + j + ' .myd').text(arrangeDay);

                        var scheduleArrange = { groupList: [] };
                        var timeList = n.ScheduleArrangeDateInfoList[j].ScheduleArrangeTimeInfoList;
                        for (var i = 0; i < timeList.length; i++) {
                            if (!scheduleArrange[timeList[i].GroupName]) {
                                scheduleArrange[timeList[i].GroupName] = {
                                    times: []
                                };
                                scheduleArrange.groupList.push(timeList[i].GroupName);
                            }

                            scheduleArrange[timeList[i].GroupName].times.push(timeList[i]);

                            if (page.currentRecommend.ScheduleDate == arrangeDay 
                                && page.currentRecommend.ScheduleTime == timeList[i].StartTime + "-" + timeList[i].EndTime) {
                                scheduleArrange.scheduleGroupName = timeList[i].GroupName;
                            }
                        }
                        page.currentRecommend.ScheduleArrangeList.push(scheduleArrange);

                        var arrangeWeek = '';
                        switch (n.ScheduleArrangeDateInfoList[j].ArrangeWeek) {
                            case 7:
                                arrangeWeek = "周日";
                                break;
                            case 1:
                                arrangeWeek = "周一";
                                break;
                            case 2:
                                arrangeWeek = "周二";
                                break;
                            case 3:
                                arrangeWeek = "周三";
                                break;
                            case 4:
                                arrangeWeek = "周四";
                                break;
                            case 5:
                                arrangeWeek = "周五";
                                break;
                            case 6:
                                arrangeWeek = "周六";
                                break;
                        }
                        $('#time_container .day_'+ j +' .week').text(arrangeWeek);
                    }
                }
            });
                
            pageEvent.addDateGroup(page.currentRecommend.ScheduleArrangeList[0]);
        },

        //添加日期下的时间组
        addDateGroup: function (scheduleArrange) {
            $('#time_container .ampm_title').html('');
            if (scheduleArrange.groupList.length > 1) {
                for (var i = 0; i < scheduleArrange.groupList.length; i++) {
                    var html = '';
                    if (scheduleArrange.scheduleGroupName === scheduleArrange.groupList[i]) {
                        html = '<div class="ampm pull-left actived_item am_img" data-groupname="' + scheduleArrange.groupList[i] + '" >' +
                                    '<span>' + scheduleArrange.groupList[i] + '</span>' +
                                '</div>';
                    } else {
                        html = '<div class="ampm pull-left pm_img" data-groupname="' + scheduleArrange.groupList[i] + '" >' +
                                    '<span>' + scheduleArrange.groupList[i] + '</span>' +
                                '</div>';
                    }

                    $('#time_container .ampm_title').append(html);
                }
            }

            //时间段
            pageEvent.addGroupTime(scheduleArrange, scheduleArrange.scheduleGroupName, page.timesPageNum)
        },

        //时间组添加时间段+1
        addGroupTime: function (scheduleArrange, groupName, page) {
            if (scheduleArrange[groupName]) {
                var times = scheduleArrange[groupName].times;
                var temp = '';
                var totalPage = Math.ceil(times.length * 1.0 / 16);
          
                for (var i = page * 16; i < times.length ; i++) {
                    var time = times[i].StartTime + "-" + times[i].EndTime;
                    switch (times[i].ScheduleTimeStatus) {
                        case 3:
                        case 4:
                            //不可预约时间
                            temp += '<div class="pull-left schedule-disable" data-uid="' + times[i].TimeDetailUID + '" data-quantity="' + times[i].Quantity + '" >' + time + '</div>';
                            break;
                        case 0://选中的时间
                            temp += '<div class="pull-left schedule-current" data-uid="' + times[i].TimeDetailUID + '" data-quantity="' + times[i].Quantity + '" >' + time + '</div>';
                            break;
                        case 1:
                            //选中的时间
                            temp += '<div class="pull-left schedule-finish" data-uid="' + times[i].TimeDetailUID + '" data-quantity="' + times[i].Quantity + '" >' + time + '</div>';
                            break;
                        default:
                            //可预约时间
                            temp += '<div class="pull-left schedule-enable" data-uid="' + times[i].TimeDetailUID + '" data-quantity="' + times[i].Quantity + '" >' + time + '</div>';
                            break;
                    }
                }
                $('#time_detail_container').html(temp);

                if (totalPage == 1) {
                    $('#uppage').css({ 'background-color': '#DFDFDF' }).attr('disable', 'true');
                    $('#nextpage').css({ 'background-color': '#DFDFDF' }).attr('disable', 'true');
                } else if (page == 0) {
                    $('#uppage').css({ 'background-color': '#DFDFDF' }).attr('disable', 'true');
                    $('#nextpage').css({ 'background-color': '#00a2ff' }).attr('disable', 'false');
                } else if (page + 1 == totalPage) {
                    $('#uppage').css({ 'background-color': '#00a2ff' }).attr('disable', 'false');
                    $('#nextpage').css({ 'background-color': '#DFDFDF' }).attr('disable', 'true');
                } else {
                    $('#uppage').css({ 'background-color': '#00a2ff' }).attr('disable', 'false');
                    $('#nextpage').css({ 'background-color': '#00a2ff' }).attr('disable', 'false');
                }
            }
        },

        //切换时间组+1
        changeGroup: function () {
            $('.ampm').removeClass('actived_item');
            $(this).addClass('actived_item');
            var scheduleArrange = page.currentRecommend.ScheduleArrangeList[page.dateIndex];
            page.timesPageNum = 0;
            pageEvent.addGroupTime(scheduleArrange, $(this).data("groupname"), page.timesPageNum);
        },

        //选中时间+1
        selectTime: function () {
            if ($(this).hasClass('schedule-enable')) {
                $('#time_container .schedule-current').removeClass('schedule-current').addClass('schedule-enable');
                $(this).removeClass('schedule-enable').addClass('schedule-current');

                page.currentRecommend.ScheduleTime = $(this).text();
                page.currentRecommend.Quantity = $(this).data('quantity');

                page.scheduleExamRequest.ChangeFlag = true;
            }
        },

        //切换日期
        changeDate: function () {
            var loading = layer.load(1, { shade: [0.4, '#333'] });//加载层

            $('.day_items.actived_item').removeClass('actived_item');
            var $this = $(this).addClass('actived_item');
            page.dateIndex = $(this).data("i");
            var date = $this.find('.myd').text();

            page.recommendInfo.scheduleDate = date;
            var scheduleArrange = page.currentRecommend.ScheduleArrangeList[page.dateIndex];
            var now = new Date();

            var isGroupName = false;
            if (date == now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate()) {
                now = new Date(date + " " + now.getHours() + ":" + now.getMinutes());

                for (var i = 0; i < scheduleArrange.groupList.length; i++) {
                    var times = scheduleArrange[scheduleArrange.groupList[i]].times;
                    
                    for (var j = 0; j < times.length; j++) {
                        var start = new Date(date + " " + times[j].StartTime);
                        var end = new Date(date + " " + times[j].EndTime);

                        if (now >= start && now <= end) {
                            scheduleArrange.scheduleGroupName = times[j].GroupName;
                            isGroupName = true;
                            break;
                        }
                    }

                    if (isGroupName) {
                        break;
                    }
                }
            }
            if (!isGroupName) {
                scheduleArrange.scheduleGroupName = scheduleArrange.groupList[0];
            }

            pageEvent.addDateGroup(scheduleArrange);
            layer.close(loading);

        },

        dateMoveLeft: function () {
            var date = $('.day_0 .myd').text();
            var now = new Date();

            if (date == now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate()) {
                return;
            }

            var startDate = new Date(date);
            startDate = startDate.setDate(startDate.getDate() - 3);
            startDate = new Date(startDate);
            startDate = startDate.getFullYear() + "-" + (startDate.getMonth() + 1) + "-" + startDate.getDate();

            page.schedule.startDate = startDate;
            page.schedule.getScheduleInfo(page.scheduleExamRequest, [], "", "", pageEvent.getScheduleInfoCallback);
        },

        dateMoveRight: function () {
            var date = $('.day_2 .myd').text();
            var startDate = new Date(date);
            startDate = startDate.setDate(startDate.getDate() + 1);
            startDate = new Date(startDate);
            startDate = startDate.getFullYear() + "-" + (startDate.getMonth() + 1) + "-" + startDate.getDate();

            page.schedule.startDate = startDate;
            page.schedule.getScheduleInfo(page.scheduleExamRequest, [], "", "", pageEvent.getScheduleInfoCallback);
        },

        //预约左右移动
        moveSchedule:function(){
            var $this = $(this);
            var type="";
            var list = [];

            switch($this.data('arrowposition')){
                case "unfinishleft":
                    if(page.unFinishList.current === 0){
                        $this.find('img').css('visibility', 'hidden');
                        return;
                    }

                    page.unFinishList.current--;
                    if(page.unFinishList.current === 0){
                        $this.find('img').css('visibility', 'hidden');
                    }
                    $('.unfinishright img').css('visibility', 'visible');
                    
                    type = "unfinish";
                    list.push(page.unFinishList[page.unFinishList.current * 2]);
                    list.push(page.unFinishList[page.unFinishList.current * 2 + 1]);
                    break;
                case "unfinishright":
                    if((page.unFinishList.current+ 1) * 2 >= page.unFinishList.length){
                        $this.find('img').css('visibility', 'hidden');
                        return;
                    }

                    page.unFinishList.current++;
                    if((page.unFinishList.current+ 1) * 2 >= page.unFinishList.length){
                        $this.find('img').css('visibility', 'hidden');
                    }
                    $('.unfinishleft img').css('visibility', 'visible');

                    type = "unfinish";
                    if(page.unFinishList.length >= (page.unFinishList.current + 1) * 2){
                        list.push(page.unFinishList[page.unFinishList.current * 2]);
                        list.push(page.unFinishList[page.unFinishList.current * 2 + 1]);
                    } else {
                        list.push(page.unFinishList[page.unFinishList.current * 2]);
                    }
                    break;
                case "finishleft":
                    if(page.finishList.current === 0){
                        $this.find('img').css('visibility', 'hidden');
                        return;
                    }

                    page.finishList.current--;
                    if(page.finishList.current === 0){
                        $this.find('img').css('visibility', 'hidden');
                    }
                    $('.finishright img').css('visibility', 'visible');
                    
                    type = "finish";
                    list.push(page.finishList[page.finishList.current * 2]);
                    list.push(page.finishList[page.finishList.current * 2 + 1]);
                    break;
                case "finishright":
                    if((page.finishList.current + 1) * 2 >= page.finishList.length){
                        $this.find('img').css('visibility', 'hidden');
                        return;
                    }

                    page.finishList.current++;
                    if ((page.finishList.current + 1) * 2 >= page.finishList.length) {
                        $this.find('img').css('visibility', 'hidden');
                    }
                    $('.finishleft img').css('visibility', 'visible');

                    type = "finish";
                    if(page.finishList.length >= (page.finishList.current + 1) * 2){
                        list.push(page.finishList[page.finishList.current * 2]);
                        list.push(page.finishList[page.finishList.current * 2 + 1]);
                    } else {
                        list.push(page.finishList[page.finishList.current * 2]);
                    }
                    
                    break;
                default:
                    break;
            }

            if(type == "finish"){
                $('#main_container .finish-container').html('');
                for(var i=0;i<list.length;i++){
                    page.bindFinish(list[i], page.finishList.current * 2 + i + 1);
                }
            } else {
                $('#main_container .un-finish-container').html('');
                for(var i=0;i<list.length;i++){
                    page.bindUnFinish(list[i], page.unFinishList.current * 2 + i + 1);
                }
            }
        },

        //选择要打印的项
        selectPrint:function(){
            var placerOrderNO = $(this).closest('.ifbook_items').data('placerorderno');
            var printExam = null;

            $.each(page.finishList, function (i, n) {
                if (n.PlacerOrderNO == placerOrderNO) {
                    n.printFlag = !n.printFlag;
                    printExam = n;
                    return false;
                }
            });

            if (printExam.printFlag) {
                page.printTotal++;
                $(this).parent().find('.print_img').show();
            } else {
                page.printTotal--;
                $(this).parent().find('.print_img').hide();
            }

            $('#print').text('打印' + (page.printTotal === 0 ? "" : "（" + page.printTotal + "）"));
        },

        //打印
        print: function () {
            var printNum = 0;
            $.each(page.finishList, function(i, n){
                if(n.printFlag) {
                    printNum++;
                    setTimeout(function(){
                        page.schedule.print(n);
                    }, 2000);
                }
            });
        },

        //未预约的保存后重新绑定
        bindAfterSave: function () {
            $('.unbooknum').text('（' + page.unFinishList.length + '）');
            $('.booknum').text('（' + page.finishList.length + '）');

            $('#main_container .finish-container').html('');
            if (page.finishList.length <= 2) {
                for (var i = 0 ; i < page.finishList.length; i++) {
                    page.bindFinish(page.finishList[i], i + 1);
                }

                $('.finishright').find('img').css('visibility', 'hidden');
                $('.finishleft').find('img').css('visibility', 'hidden');
            } else {
                page.finishList.current = Math.ceil(page.finishList.length * 1.0 / 2);
                for (var i = ((page.finishList.current - 1) * 2) ; i < page.finishList.length; i++) {
                    page.bindFinish(page.finishList[i], i + 1);
                }

                $('.finishright').find('img').css('visibility', 'hidden');
                $('.finishleft').find('img').css('visibility', 'visible');
            }

            $('#main_container .un-finish-container').html('');
            if (page.unFinishList.length <= 2) {
                for (var i = 0 ; i < page.unFinishList.length; i++) {
                    page.bindUnFinish(page.unFinishList[i], i + 1);
                }
                $('.unfinishright').find('img').css('visibility', 'hidden');
                $('.unfinishleft').find('img').css('visibility', 'hidden');
            } else {
                if (page.unFinishList.length > (page.unFinishList.current + 1) * 2) {
                    page.bindUnFinish(page.unFinishList[page.unFinishList.current * 2], page.unFinishList.current * 2 + 1);
                    page.bindUnFinish(page.unFinishList[page.unFinishList.current * 2 + 1], page.unFinishList.current * 2 + 2);

                    $('.unfinishright').find('img').css('visibility', 'visible');
                    $('.unfinishleft').find('img').css('visibility', 'visible');
                } else if (page.unFinishList.length > page.unFinishList.current * 2 && page.unFinishList.length < (page.unFinishList.current + 1) * 2) {
                    page.bindUnFinish(page.unFinishList[page.unFinishList.current * 2], page.unFinishList.current * 2 + 1);

                    $('.unfinishright').find('img').css('visibility', 'hidden');
                    $('.unfinishleft').find('img').css('visibility', 'visible');
                } else {
                    if (page.unFinishList.current > 0) {
                        page.unFinishList.current--;
                        page.bindUnFinish(page.unFinishList[page.unFinishList.current * 2], page.unFinishList.current * 2 + 1);
                        page.bindUnFinish(page.unFinishList[page.unFinishList.current * 2 + 1], page.unFinishList.current * 2 + 2);

                        $('.unfinishright').find('img').css('visibility', 'hidden');
                        $('.unfinishleft').find('img').css('visibility', 'visible');
                    }
                }
            }
        }
    };
    page.init();
})();
